import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Shoppingcar from "../views/Shoppingcar.vue"
import List from "../views/List.vue"
import Login from "../components/lorg/login.vue"
import Reg from "../components/lorg/reg.vue"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: "/pay",
    name: "pay",
    component: () => import("../views/Pay.vue"),
    beforeEnter: (to, from, next) => {
      if (from.name == "order") {
        next()
      } else {
        next("/order")
      }
    }
  },
  {
    path: "/dedail/:id",
    name: "dedail",
    component: () => import("../views/Dedail.vue")
  },
  {
    path: "/order",
    name: "order",
    component: () => import("../views/Order.vue"),
    beforeEnter: (to, from, next) => {
      if (from.name == "shoppingcar") {
        next()
      } else {
        next("/shoppingcar")
      }
    }
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import("../views/Mine.vue"),
    children: [
      {
        path: "",
        component: Login
      },
      {
        path: 'login',
        name: "login",
        component: Login

      },
      {
        path: "reg",
        name: "reg",
        component: Reg
      }


    ]
  },
  {
    path: "/list",
    name: "list",
    component: List
  },
  {
    path: '/shoppingcar',
    name: 'shoppingcar',
    component: Shoppingcar,
    meta: {
      isLogin: true
    }

  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]


const router = new VueRouter({
  routes
})

//全局前置守卫
router.beforeEach((to, from, next) => {
  console.log(from)
  if (to.meta && to.meta.isLogin) {
    if (window.localStorage.getItem("username")) {
      next()
    } else {
      next("/mine")
    }
  }
  next()
})


export default router
